package com.mz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mz.dao.OmsGoodsCartDao;
import com.mz.dao.OmsGoodsInfoDao;
import com.mz.entity.OmsGoodsCart;
import com.mz.entity.OmsGoodsInfo;
import com.mz.entity.vo.CartSelectedVo;
import com.mz.entity.vo.GoodsToCartVo;
import com.mz.service.OmsGoodsCartService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * (OmsGoodsCart)表服务实现类
 *
 * @author makejava
 * @since 2023-12-13 13:30:32
 */
@Service
public class OmsGoodsCartServiceImpl  implements OmsGoodsCartService {

    @Resource
    private OmsGoodsCartDao omsGoodsCartDao;
    @Resource
    private OmsGoodsInfoDao omsGoodsInfoDao;

    @Override
    public List<OmsGoodsCart> getCartList( Long userId ) throws Exception {

        LambdaQueryWrapper<OmsGoodsCart> lqs = new LambdaQueryWrapper<>();
        lqs.eq(OmsGoodsCart::getUserId, userId);
        List<OmsGoodsCart> omsGoodsCarts = omsGoodsCartDao.selectList(lqs);
        if( omsGoodsCarts == null || omsGoodsCarts.size() ==0){
            return new ArrayList<OmsGoodsCart>();
        }
        Set<Long> goodsIdSet = omsGoodsCarts.stream().map(item -> {
            return item.getGoodsId();
        }).collect(Collectors.toSet());
        List<OmsGoodsInfo> omsGoodsInfos = omsGoodsInfoDao.selectBatchIds(goodsIdSet);
        if( omsGoodsInfos == null  || omsGoodsInfos.size() == 0){
            return  omsGoodsCarts;
        }
        for( OmsGoodsCart omsGoodsCart : omsGoodsCarts ){
            OmsGoodsInfo omsGoodsInfo = omsGoodsInfos.stream().filter(item -> {
                return item.getId().equals(omsGoodsCart.getGoodsId());
            }).findFirst().get();
            omsGoodsCart.setGoodsInfo(omsGoodsInfo);
        }
        return omsGoodsCarts;
    }

    @Override
    public List<OmsGoodsCart> getSelectedCart(Long userId) throws Exception {

        LambdaQueryWrapper<OmsGoodsCart> lqs = new LambdaQueryWrapper<>();
        lqs.eq(OmsGoodsCart::getUserId, userId);
        lqs.eq(OmsGoodsCart::getSelected, 1);
        List<OmsGoodsCart> omsGoodsCarts = omsGoodsCartDao.selectList(lqs);
        if( omsGoodsCarts == null || omsGoodsCarts.size() ==0){
            return new ArrayList<OmsGoodsCart>();
        }
        Set<Long> goodsIdSet = omsGoodsCarts.stream().map(item -> {
            return item.getGoodsId();
        }).collect(Collectors.toSet());
        List<OmsGoodsInfo> omsGoodsInfos = omsGoodsInfoDao.selectBatchIds(goodsIdSet);
        if( omsGoodsInfos == null  || omsGoodsInfos.size() == 0){
            return  omsGoodsCarts;
        }
        for( OmsGoodsCart omsGoodsCart : omsGoodsCarts ){
            OmsGoodsInfo omsGoodsInfo = omsGoodsInfos.stream().filter(item -> {
                return item.getId().equals(omsGoodsCart.getGoodsId());
            }).findFirst().get();
            omsGoodsCart.setGoodsInfo(omsGoodsInfo);
        }
        return omsGoodsCarts;
    }


    @Override
    @Transactional
    public void addCart(GoodsToCartVo cartVo) throws Exception {

        synchronized ( cartVo.getUserId().toString().intern() ){

            //先获取是否存在
            LambdaQueryWrapper<OmsGoodsCart> lqs = new LambdaQueryWrapper<>();
            lqs.eq(OmsGoodsCart::getGoodsId,cartVo.getGoodsId());
            lqs.eq(OmsGoodsCart::getUserId, cartVo.getUserId());
            OmsGoodsCart omsGoodsCart = omsGoodsCartDao.selectOne(lqs);

            //如果不存在
            if( omsGoodsCart == null ){
                omsGoodsCart = new OmsGoodsCart();
                omsGoodsCart.setGoodsId(cartVo.getGoodsId())
                        .setUserId(cartVo.getUserId())
                        .setAmount(1);
                omsGoodsCartDao.insert(omsGoodsCart);
                return;
            }

            //总数量
            omsGoodsCart.setAmount( 1 + omsGoodsCart.getAmount() );
            omsGoodsCartDao.updateById(omsGoodsCart);
            return;
        }

    }

    @Override
    @Transactional
    public void updateCartAmountAndStatus(List<CartSelectedVo> cartSelectedVoList) throws Exception {
        if( cartSelectedVoList == null || cartSelectedVoList.size() == 0 ){
            return;
        }

        for( CartSelectedVo cartSelectedVo : cartSelectedVoList ){
            OmsGoodsCart omsGoodsCart = new OmsGoodsCart();
            omsGoodsCart.setId( cartSelectedVo.getCartId() );
            omsGoodsCart.setAmount(cartSelectedVo.getAmount());
            omsGoodsCart.setSelected(cartSelectedVo.getSelected());
            omsGoodsCartDao.updateById(omsGoodsCart);
        }
        return;
    }

    @Override
    public Integer getCartGoodsAmount(Long userId) throws Exception {

        LambdaQueryWrapper<OmsGoodsCart> lqs = new LambdaQueryWrapper<>();
        lqs.eq(OmsGoodsCart::getUserId, userId);
        List<OmsGoodsCart> omsGoodsCarts = omsGoodsCartDao.selectList(lqs);
        if( omsGoodsCarts == null || omsGoodsCarts.size() == 0 ){
            return  0;
        }
        Integer amount = 0;
        for( OmsGoodsCart omsGoodsCart: omsGoodsCarts ){
           amount += omsGoodsCart.getAmount();
        }
        return amount;
    }


}

